// ActionScript file

package zakladki.AS3 {
import AS3.constants.combo.Clothes;
import AS3.constants.combo.ElectricityConsumption;
import AS3.constants.combo.FurnitureElectronic;
import AS3.constants.combo.Income;
import AS3.constants.combo.Packaging;
import AS3.constants.combo.Recycling;

import flash.sampler._getInvocationCount;

import mx.controls.Alert;

import mx.controls.ComboBox;
import mx.controls.CheckBox;
import mx.controls.NumericStepper;
import mx.core.FlexGlobals;


public class ConsumptionChangeListener {

    private var incomeCombo:ComboBox;
    private var clothesCombo:ComboBox;
    private var packagingCombo:ComboBox;
    private var furnitureElectronicCombo:ComboBox;
    private var electricityCombo:ComboBox;
    private var recreationCombo:ComboBox;
    private var recyclingCombo:ComboBox;

    private var tvCheckBox:CheckBox;
    private var videoDVDCheckBox:CheckBox;
    private var hiFiCheckBox:CheckBox;
    private var computerCheckBox:CheckBox;
    private var cableTVCheckBox:CheckBox;
    private var electricLightingCheckBox:CheckBox;
    private var underwearDryerCheckBox:CheckBox;

    private var renewableRatioStepper:NumericStepper;

    private var rootApp:Fossilminator = FlexGlobals.topLevelApplication as Fossilminator;


    public function ConsumptionChangeListener(incomeCombo:ComboBox, clothesCombo:ComboBox, packagingCombo:ComboBox, furnitureElectronicCombo:ComboBox, electricityCombo:ComboBox, recreationCombo:ComboBox, recyclingCombo:ComboBox, tvCheckBox:CheckBox, videoDVDCheckBox:CheckBox, hiFiChechBox:CheckBox, computerCheckBox:CheckBox, cableTVCheckBox:CheckBox, electricLightingCheckBox:CheckBox, underwearDryerCheckBox:CheckBox, renewableRatioStepper:NumericStepper) {
        this.incomeCombo = incomeCombo;
        this.clothesCombo = clothesCombo;
        this.packagingCombo = packagingCombo;
        this.furnitureElectronicCombo = furnitureElectronicCombo;
        this.electricityCombo = electricityCombo;
        this.recreationCombo = recreationCombo;
        this.recyclingCombo = recyclingCombo;
        this.tvCheckBox = tvCheckBox;
        this.videoDVDCheckBox = videoDVDCheckBox;
        this.hiFiCheckBox = hiFiChechBox;
        this.computerCheckBox = computerCheckBox;
        this.cableTVCheckBox = cableTVCheckBox;
        this.electricLightingCheckBox = electricLightingCheckBox;
        this.underwearDryerCheckBox = underwearDryerCheckBox;
        this.renewableRatioStepper = renewableRatioStepper;

        try {

//            Alert.show(new String(incomeCombo != null));
//            Alert.show(new String(clothesCombo != null));
//            Alert.show(new String(packagingCombo != null));
//            Alert.show(new String(furnitureElectronicCombo != null));
//            Alert.show(new String(electricityCombo != null));
//            Alert.show(new String(recreationCombo != null));
//            Alert.show(new String(recyclingCombo != null));
//
//            Alert.show(new String(tvCheckBox != null));
//            Alert.show(new String(videoDVDCheckBox != null));
//            Alert.show(new String(hiFiChechBox != null));
//
//            Alert.show(new String(computerCheckBox != null));
//            Alert.show(new String(cableTVCheckBox != null));
//            Alert.show(new String(electricLightingCheckBox != null));
//
//            Alert.show(new String(underwearDryerCheckBox != null));
//            Alert.show(new String(renewableRatioStepper != null));

            var selectedData:ConsumptionSelectedData = createSelectedData();
            this.initializeConsumptionContentView(selectedData);

        } catch(e:Error) {
            Alert.show("initialize view: " + e.message);
        }
    }

    private function createSelectedData():ConsumptionSelectedData {
        return new ConsumptionSelectedData(getComboData(incomeCombo), getComboData(recyclingCombo), getComboData(furnitureElectronicCombo), getComboData(electricityCombo),
                getComboData(packagingCombo), getComboData(clothesCombo), getComboData(recreationCombo),
                tvCheckBox.selected, videoDVDCheckBox.selected, cableTVCheckBox.selected,
                computerCheckBox.selected, hiFiCheckBox.selected, electricLightingCheckBox.selected, underwearDryerCheckBox.selected,
                renewableRatioStepper.value);
    }

    private function getComboData(combo:ComboBox):int {
        var result:int = combo.selectedItem.data;
        return result;

    }

    public function incomeChanged(calc:Boolean):void {
        var selectedData:ConsumptionSelectedData = createSelectedData();
        changeIncomeView(selectedData.income);
        if (calc) {
            updatePerson(selectedData);
            rootApp.calculate();
        }
    }

    public function clothesChanged(obliczaj:Boolean):void {
        var selectedData:ConsumptionSelectedData = createSelectedData();
        changeClothesView(selectedData.clothes);
        if (obliczaj) {
            updatePerson(selectedData);
            rootApp.calculate();
        }
    }

    public function packagingChanged(obliczaj:Boolean):void {
        var selectedData:ConsumptionSelectedData = createSelectedData();
        changePackagingView(selectedData.packaging);
        if (obliczaj) {
            updatePerson(selectedData);
            rootApp.calculate();
        }
    }

    public function furnitureElectronicChanged(obliczaj:Boolean):void {
        var selectedData:ConsumptionSelectedData = createSelectedData();

        changeFurnitureElectronicView(selectedData.furnitureElectronics);

        agdEquipmentChanged(selectedData);

        if (obliczaj) {
            updatePerson(selectedData);
            rootApp.calculate();
        }
    }


    public function recreationChanged(obliczaj:Boolean):void {
        var selectedData:ConsumptionSelectedData = createSelectedData();

        changeRecreationView(selectedData.recreation);
        agdEquipmentChanged(selectedData);
        if (obliczaj) {
            updatePerson(selectedData);
            rootApp.calculate();
        }
    }

    public function electricityChanged(obliczaj:Boolean):void {
        var selectedData:ConsumptionSelectedData = createSelectedData();

        agdEquipmentChanged(selectedData);
        if (obliczaj) {
            updatePerson(selectedData);
            rootApp.calculate();
        }
    }

    public function recycklingChanged(obliczaj:Boolean):void {
        var selectedData:ConsumptionSelectedData = createSelectedData();

        changeRecyclingView(selectedData.recycling);
        if (obliczaj) {
            updatePerson(selectedData);
            rootApp.calculate();
        }
    }

    public function lightingChanged(obliczaj:Boolean):void {
        var selectedData:ConsumptionSelectedData = createSelectedData();

        changeLighting(selectedData.electricLightningSelected);
        if (obliczaj) {
            updatePerson(selectedData);
            rootApp.calculate();
        }
    }

    public function underwearDryerChanged(obliczaj:Boolean):void {
        var selectedData:ConsumptionSelectedData = createSelectedData();

        if (obliczaj) {
            updatePerson(selectedData);
            rootApp.calculate();
        }
    }

    public function tvChanged(obliczaj:Boolean):void {
        var selectedData:ConsumptionSelectedData = createSelectedData();

        changeTVView(selectedData.tvSelected, selectedData.furnitureElectronics, selectedData.electricity);
        if (obliczaj) {
            updatePerson(selectedData);
            rootApp.calculate();
        }
    }

    public function videoDVDChanged(obliczaj:Boolean):void {
        var selectedData:ConsumptionSelectedData = createSelectedData();

        changeDVDView(selectedData.videoDVDSelected, selectedData.furnitureElectronics, selectedData.electricity);
        if (obliczaj) {
            updatePerson(selectedData);
            rootApp.calculate();
        }
    }

    public function cableTVChanged(obliczaj:Boolean):void {
        var selectedData:ConsumptionSelectedData = createSelectedData();

        changeCableTvView(selectedData.cableTVSelected);
        if (obliczaj) {
            updatePerson(selectedData);
            rootApp.calculate();
        }
    }

    public function computerChanged(obliczaj:Boolean):void {
        var selectedData:ConsumptionSelectedData = createSelectedData();

        changeComputerView(selectedData.computeSelected, selectedData.furnitureElectronics, selectedData.electricity);
        if (obliczaj) {
            updatePerson(selectedData);
            rootApp.calculate();
        }
    }

    public function hiFiChanged(obliczaj:Boolean):void {
        var selectedData:ConsumptionSelectedData = createSelectedData();

        changeHiFiView(selectedData.radioHiFiSelected, selectedData.furnitureElectronics, selectedData.electricity);
        if (obliczaj) {
            updatePerson(selectedData);
            rootApp.calculate();
        }
    }

    public function renewableRatioChanged(obliczaj:Boolean):void {
        var selectedData:ConsumptionSelectedData = createSelectedData();
        if (obliczaj) {
            updatePerson(selectedData);
            rootApp.calculate();
        }
    }

    private function agdEquipmentChanged(selectedData:ConsumptionSelectedData):void {
        changeTVView(selectedData.tvSelected, selectedData.furnitureElectronics, selectedData.electricity);
        changeHiFiView(selectedData.radioHiFiSelected, selectedData.furnitureElectronics, selectedData.electricity);
        changeDVDView(selectedData.videoDVDSelected, selectedData.furnitureElectronics, selectedData.electricity);
        changeComputerView(selectedData.computeSelected, selectedData.furnitureElectronics, selectedData.electricity);
    }

    public function initializeConsumptionContentView(selectedData:ConsumptionSelectedData):void {
        changeIncomeView(selectedData.income);
        changeRecyclingView(selectedData.recycling);
        changeTVView(selectedData.tvSelected, selectedData.furnitureElectronics, selectedData.electricity);
        changePackagingView(selectedData.packaging);
        changeClothesView(selectedData.clothes);
        changeHiFiView(selectedData.radioHiFiSelected, selectedData.furnitureElectronics, selectedData.electricity);
        changeRecreationView(selectedData.recreation);
        changeDVDView(selectedData.videoDVDSelected, selectedData.furnitureElectronics, selectedData.electricity);
        changeLighting(selectedData.electricLightningSelected);
        changeCableTvView(selectedData.cableTVSelected);
        changeFurnitureElectronicView(selectedData.furnitureElectronics);
        changeComputerView(selectedData.computeSelected, selectedData.furnitureElectronics, selectedData.electricity);
    }

    private function changeIncomeView(income:int):void {


        //Ukrywamy grafikę pieniędzy
        rootApp.widok_sceny.Pok.PieniadzeBMalo.visible = false;
        rootApp.widok_sceny.Pok.PieniadzeMalo.visible = false;
        rootApp.widok_sceny.Pok.PieniadzeSrednio.visible = false;
        rootApp.widok_sceny.Pok.PieniadzeDuzo.visible = false;
        rootApp.widok_sceny.Pok.PieniadzeBDuzo.visible = false;


        if (income == Income.AVERAGE_10_PER_CENT || income == Income.AVERAGE_20_PER_CENT) {
            rootApp.widok_sceny.Pok.PieniadzeBMalo.visible = true;
        }
        else if (income == Income.AVERAGE_50_PER_CENT || income == Income.AVERAGE_80_PER_CENT) {
            rootApp.widok_sceny.Pok.PieniadzeMalo.visible = true;
        }
        else if (income == Income.AVERAGE || income == Income.AVERAGE_120_PER_CENT) {
            rootApp.widok_sceny.Pok.PieniadzeSrednio.visible = true;
        }
        else if (income == Income.AVERAGE_150_PER_CENT || income == Income.AVERAGE_200_PER_CENT) {
            rootApp.widok_sceny.Pok.PieniadzeDuzo.visible = true;
        }
        else {
            rootApp.widok_sceny.Pok.PieniadzeBDuzo.visible = true;
        }
    }

    private function changeRecyclingView(recycling:int):void {
        rootApp.widok_sceny.Pok.RecyklingPusto.visible = false;
        rootApp.widok_sceny.Pok.RecyklingMalo.visible = false;
        rootApp.widok_sceny.Pok.RecyklingSrednio.visible = false;
        rootApp.widok_sceny.Pok.RecyklingDuzo.visible = false;

        switch (recycling) {
            case Recycling.NO_RECYCLING:
                rootApp.widok_sceny.Pok.RecyklingPusto.visible = true;
                break;
            case Recycling.SMALL_PART:
                rootApp.widok_sceny.Pok.RecyklingMalo.visible = true;
                break;
            case Recycling.FAIR_PART:
                rootApp.widok_sceny.Pok.RecyklingSrednio.visible = true;
                break;
            case Recycling.LARGE_PART:
                rootApp.widok_sceny.Pok.RecyklingDuzo.visible = true;
                break;
        }
    }

    private function changeTVView(tvSelected:Boolean, furnitureElectronic:int, electricity:int):void {
        rootApp.widok_sceny.Pok.TVEkranHigh.visible = false;
        rootApp.widok_sceny.Pok.TVEkranNormal.visible = false;
        rootApp.widok_sceny.Pok.TVEkranOut.visible = false;
        rootApp.widok_sceny.Pok.TVLampka.visible = false;
        rootApp.widok_sceny.Pok.TVMetka.visible = false;
        rootApp.widok_sceny.Pok.TVNormal.visible = false;
        rootApp.widok_sceny.Pok.TVUzywane.visible = false;


        if (tvSelected) {
            rootApp.widok_sceny.Pok.TVLampka.visible = true;
            rootApp.widok_sceny.Pok.TVMetka.visible = false;

            switch (furnitureElectronic) {
                case FurnitureElectronic.SECOND_HAND:
                    rootApp.widok_sceny.Pok.TVUzywane.visible = true;
                    break;
                case FurnitureElectronic.USUALLY_NEW:
                    rootApp.widok_sceny.Pok.TVNormal.visible = true;
                    break;
                case FurnitureElectronic.FASHIONABLE:
                    rootApp.widok_sceny.Pok.TVMetka.visible = true;
                    rootApp.widok_sceny.Pok.TVNormal.visible = true;
                    break;
            }
            switch (electricity) {
                case ElectricityConsumption.NO_REDUCTION:
                    rootApp.widok_sceny.Pok.TVEkranHigh.visible = true;
                    break;
                case ElectricityConsumption.AVERAGE:
                    rootApp.widok_sceny.Pok.TVEkranNormal.visible = true;
                    break;
                case ElectricityConsumption.SAVING:
                    rootApp.widok_sceny.Pok.TVEkranOut.visible = true;
                    rootApp.widok_sceny.Pok.TVLampka.visible = false;
                    break;
            }
        }


    }

    private function changePackagingView(packaging:int):void {
        rootApp.widok_sceny.Pok.OpakowanieNowe.visible = false;
        rootApp.widok_sceny.Pok.OpakowanieSrednie.visible = false;
        rootApp.widok_sceny.Pok.OpakowanieMinimum.visible = false;

        switch (packaging) {
            case Packaging.MINIMAL:
                rootApp.widok_sceny.Pok.OpakowanieMinimum.visible = true;
                break;
            case Packaging.ALL:
                rootApp.widok_sceny.Pok.OpakowanieSrednie.visible = true;
                break;
            case Packaging.FANCY:
                rootApp.widok_sceny.Pok.OpakowanieNowe.visible = true;
                break;
        }
    }

    private function changeClothesView(clothes:int):void {
        rootApp.widok_sceny.Pok.UbraniaNowe.visible = false;
        rootApp.widok_sceny.Pok.UbraniaSrednie.visible = false;
        rootApp.widok_sceny.Pok.UbraniaUzywane.visible = false;

        switch (clothes) {
            case Clothes.SECOND_HAND:
                rootApp.widok_sceny.Pok.UbraniaUzywane.visible = true;
                break;
            case Clothes.NEW_WHEN_WORN_OUT:
                rootApp.widok_sceny.Pok.UbraniaSrednie.visible = true;
                break;
            case Clothes.ALWAYS_NEW:
                rootApp.widok_sceny.Pok.UbraniaNowe.visible = true;
                break;
        }
    }

    private function changeRecreationView(recreation:int):void {
        rootApp.widok_sceny.Pok.Rekreacja1.visible = false;
        rootApp.widok_sceny.Pok.Rekreacja2.visible = false;
        rootApp.widok_sceny.Pok.Rekreacja3.visible = false;
        switch (recreation) {
            case 0:
                rootApp.widok_sceny.Pok.Rekreacja1.visible = true;
                break;
            case 1:
                rootApp.widok_sceny.Pok.Rekreacja2.visible = true;
                break;
            case 2:
                rootApp.widok_sceny.Pok.Rekreacja3.visible = true;
                break;
        }
    }

    private function changeDVDView(videoDVDSelected:Boolean, furnitureElectronic:int, electricity:int):void {
        rootApp.widok_sceny.Pok.DVDUzywany.visible = false;
        rootApp.widok_sceny.Pok.DVDLampka.visible = false;
        rootApp.widok_sceny.Pok.DVDMetka.visible = false;
        rootApp.widok_sceny.Pok.DVDNormalny.visible = false;

        if (videoDVDSelected) {
            switch (furnitureElectronic) {
                case FurnitureElectronic.SECOND_HAND:
                    rootApp.widok_sceny.Pok.DVDUzywany.visible = true;
                    break;
                case FurnitureElectronic.USUALLY_NEW:
                    rootApp.widok_sceny.Pok.DVDNormalny.visible = true;
                    break;
                case FurnitureElectronic.FASHIONABLE:
                    rootApp.widok_sceny.Pok.DVDMetka.visible = true;
                    rootApp.widok_sceny.Pok.DVDNormalny.visible = true;
                    break;
            }
            if (electricity != ElectricityConsumption.SAVING) {
                rootApp.widok_sceny.Pok.DVDLampka.visible = true;
            }
        }
    }

    private function changeLighting(lightingSelected:Boolean):void {
        rootApp.widok_sceny.Pok.Oswietlenie.visible = lightingSelected;
    }

    private function changeCableTvView(TVKablowaLubSatelitarna:Boolean):void {
        rootApp.widok_sceny.Pok.Satelita.visible = TVKablowaLubSatelitarna;
    }

    private function changeFurnitureElectronicView(furnitureElectronic:int):void {
        rootApp.widok_sceny.Pok.MebleUzywane.visible = furnitureElectronic == FurnitureElectronic.SECOND_HAND;
    }

    public function changeComputerView(computerSelected:Boolean, furnitureElectronic:int, electricity:int):void {
        if (computerSelected) {
            rootApp.widok_sceny.Pok.KompMetka.visible = false;
            rootApp.widok_sceny.Pok.KompNormalny.visible = false;
            rootApp.widok_sceny.Pok.KompUzywany.visible = false;
            rootApp.widok_sceny.Pok.KompEkranHigh.visible = false;
            rootApp.widok_sceny.Pok.KompEkranNormal.visible = false;
            rootApp.widok_sceny.Pok.KompEkranOut.visible = false;
            rootApp.widok_sceny.Pok.KompLampka.visible = true;

            switch (furnitureElectronic) {
                case FurnitureElectronic.SECOND_HAND:
                    rootApp.widok_sceny.Pok.KompUzywany.visible = true;
                    break;
                case FurnitureElectronic.USUALLY_NEW:
                    rootApp.widok_sceny.Pok.KompNormalny.visible = true;
                    break;
                case FurnitureElectronic.FASHIONABLE:
                    rootApp.widok_sceny.Pok.KompMetka.visible = true;
                    rootApp.widok_sceny.Pok.KompNormalny.visible = true;
                    break;
            }
            switch (electricity) {
                case ElectricityConsumption.NO_REDUCTION:
                    rootApp.widok_sceny.Pok.KompEkranHigh.visible = true;
                    break;
                case ElectricityConsumption.AVERAGE:
                    rootApp.widok_sceny.Pok.KompEkranNormal.visible = true;
                    break;
                case ElectricityConsumption.SAVING:
                    rootApp.widok_sceny.Pok.KompEkranOut.visible = true;
                    rootApp.widok_sceny.Pok.KompLampka.visible = false;
                    break;
            }
        }
        else {
            rootApp.widok_sceny.Pok.KompMetka.visible = false;
            rootApp.widok_sceny.Pok.KompNormalny.visible = false;
            rootApp.widok_sceny.Pok.KompUzywany.visible = false;
            rootApp.widok_sceny.Pok.KompEkranHigh.visible = false;
            rootApp.widok_sceny.Pok.KompEkranNormal.visible = false;
            rootApp.widok_sceny.Pok.KompEkranOut.visible = false;
            rootApp.widok_sceny.Pok.KompLampka.visible = false;
        }
    }

    private function changeHiFiView(hiFiSelected:Boolean, urnitureElectronic:int, electricity:int):void {
        rootApp.widok_sceny.Pok.MuzaUzywane.visible = false;
        rootApp.widok_sceny.Pok.MuzaLampka.visible = false;
        rootApp.widok_sceny.Pok.MuzaMetka.visible = false;
        rootApp.widok_sceny.Pok.MuzaNormal.visible = false;
        if (hiFiSelected) {

            switch (urnitureElectronic) {
                case FurnitureElectronic.SECOND_HAND:
                    rootApp.widok_sceny.Pok.MuzaUzywane.visible = true;
                    break;
                case FurnitureElectronic.USUALLY_NEW:
                    rootApp.widok_sceny.Pok.MuzaNormal.visible = true;
                    break;
                case FurnitureElectronic.FASHIONABLE:
                    rootApp.widok_sceny.Pok.MuzaMetka.visible = true;
                    rootApp.widok_sceny.Pok.MuzaNormal.visible = true;
                    break;
            }
            if (electricity != ElectricityConsumption.SAVING) {
                rootApp.widok_sceny.Pok.MuzaLampka.visible = true;
            }
        }
    }


    private function updatePerson(selectedData:ConsumptionSelectedData):void {
        rootApp.updateConsumption(selectedData);
    }

}
}

